﻿using System;
using System.Collections.Generic;
using System.Text;
using KaiXin001er.Objects;
using KaiXin001er.WebBrowser;
using KaiXin001er.UTIL;
using System.Net;

namespace KaiXin001er.Logic
{
    public class Parker : BasePlayer
    {
        private int minMoneytoMove = 1000;//挪车最少收益
        List<Car> needParkList = new List<Car>();
        List<Car> needentParkList = new List<Car>();
        bool canStopOnNeighbor = false;
        public Parker(Account acct) : base(acct) { }
        ParkUserData _parkUserData;
        ParkFriendData[] _parkFriendData;
        string _verify = "";
        //public List<Friend> friends;
        string _acc = "";
        private ParkFriendData findParkFriendDataByID(string id)
        {
            foreach (ParkFriendData pfd in _parkFriendData)
            {
                if (pfd.Uid == id) return pfd;
            }
            return null;
        }
        public override bool SignOn()
        {
            //登陆首页 察看是否能停在邻居车位上

            string retstring = dac.Request(URLS.争车位首页, "", WebAccessor.RequestMethod.GetMethod);
            ///是否可以停邻居 将控制程序执行停在邻居的操作
            canStopOnNeighbor = 页面判断者.能停邻居吗(retstring);
            ///自己的信息
            this._parkUserData = 页面分析者.ReadParkUserData(retstring);
            OutPutParkUserData(_parkUserData);
            ///好友的车位满与不满的信息
            this._parkFriendData = 页面分析者.ReadParkFriendData(retstring);
            ///verify信息
            this._verify = 页面分析者.争车位g_verify(retstring);
            //计算acc的值 这地方很牛X
            this._acc = 页面分析者.计算Acc(retstring);
            return true;
        }

        private void OutPutParkUserData(ParkUserData _pUserData)
        {
            this.RaiseMsg(string.Format("[停车模块] {0}{1} 停车收入{2}元每分钟 [我的现金现金]{3}元", _pUserData.User.Real_name, _pUserData.User.Avenue, _parkUserData.Config.Money_minute, _pUserData.User.Cash));
            foreach (ParkUserDataParking p in _pUserData.Parking)
                this.RaiseMsg(string.Format("           {0}的{1}(价格{2}元)目前停在我的{3}号{4}车位上,预计收入{5}元", p.Car_real_name, p.Car_name, p.Car_price, p.Parkid, p.isFreePark ? "免费" : "私家", p.Car_profit));
            if (_pUserData.Car != null)
            {
                foreach (ParkUserDataCar car in _pUserData.Car)
                {
                    this.RaiseMsg(string.Format("           我的{0}(价格{1}元)目前停在{2}的{3}车位上,预计收入{4}元", car.Car_name, car.Price, car.Park_real_name, "私家", car.Park_profit));
                }
            }

        }

        public override bool SignOff()
        {
            return true;
        }

        public override bool PlayPlay()
        {
            //停车操作
            if (Account.IsPark) parkPlayPlay();
            if (Account.IsRace) MatchPlayPlay();
            return true;
        }
        #region 停车方法系列
        public void parkPlayPlay()
        {

            bool 至少成功停车一辆 = true;
            bool 至少有一辆车停车失败 = true;
            while ((至少成功停车一辆) && (至少有一辆车停车失败))
            {
                至少成功停车一辆 = false;
                至少有一辆车停车失败 = false;
                for (int i = 0; i < _parkUserData.Car.Length; i++)
                {
                    if ((_parkUserData.Car[i].Park_profit < minMoneytoMove) && (_parkUserData.Car[i].Park_profit != 0)) continue;
                    if (停在收费车位(_parkUserData.Car[i]))
                    {
                        至少成功停车一辆 = true;
                    }
                    else
                    {
                        至少有一辆车停车失败 = true;
                    }
                }
                if ((!至少成功停车一辆) && (至少有一辆车停车失败))
                {
                    for (int i = 0; i < _parkUserData.Car.Length; i++)
                    {
                        if (_parkUserData.Car[i].Park_profit < minMoneytoMove) continue;
                        if (停在免费车位(_parkUserData.Car[i]))
                        {
                            至少成功停车一辆 = true; break;
                        }
                        else
                        {

                            至少有一辆车停车失败 = true;
                        }
                    }
                }
            }
            //至少成功停车一辆 = true;
            //至少有一辆车停车失败 = true;
            //while ((至少成功停车一辆) && (至少有一辆车停车失败))
            //{
            //    至少成功停车一辆 = false;
            //    至少有一辆车停车失败 = false;
            //    for (int i = 0; i < _parkUserData.Car.Length; i++)
            //    {
            //        if (_parkUserData.Car[i].Park_profit < minMoneytoMove) continue;
            //        if (停在免费车位(_parkUserData.Car[i]))
            //        {
            //            至少成功停车一辆 = true;
            //        }
            //        else
            //        {

            //            至少有一辆车停车失败 = true;
            //        }
            //    }
            //}


        }

        private bool 停在收费车位(ParkUserDataCar parkUserDataCar)
        {
            if (停在邻居收费车位(parkUserDataCar)) return true;
            if (停在好友收费车位(parkUserDataCar)) return true;
            return false;
        }

        private bool 停在好友收费车位(ParkUserDataCar parkUserDataCar)
        {
            return 停车(parkUserDataCar, false, false);
        }

        private bool 停在邻居收费车位(ParkUserDataCar parkUserDataCar)
        {
            return 停车(parkUserDataCar, false, true);
        }
        private bool 停在免费车位(ParkUserDataCar parkUserDataCar)
        {
            if (停在邻居免费车位(parkUserDataCar)) return true;
            if (停在好友免费车位(parkUserDataCar)) return true;
            return false;
        }

        private bool 停在好友免费车位(ParkUserDataCar parkUserDataCar)
        {
            return 停车(parkUserDataCar, true, false);
        }

        private bool 停在邻居免费车位(ParkUserDataCar parkUserDataCar)
        {
            return 停车(parkUserDataCar, true, true);
        }

        private bool 停车(ParkUserDataCar parkUserDataCar, bool isFree, bool isNeighbor)
        {
            bool result = false;
            foreach (ParkFriendData pfd in _parkFriendData)
            {
                if (pfd.Full == "1") continue;//如果满了就直接进行下一个
                if ((pfd.Neighbor == 1) == isNeighbor)
                {
                    string ret = "";
                    if (isNeighbor) ret = dac.PostRequest(URLS.邻居车位场景, "verify=" + _verify + "&puid=" + pfd.Uid);
                    else ret = dac.PostRequest(URLS.好友车位场景, "verify=" + _verify + "&puid=" + pfd.Uid);
                    ParkUserData pud = 页面分析者.ReadParkUserData(ret);
                    //OutPutParkUserData(pud);
                    foreach (ParkUserDataParking park in pud.CanParking)
                    {
                        if (park.isFreePark == isFree)
                        {
                            //停车
                            请求参数 r = new 请求参数();
                            r.add("verify", this._verify);
                            r.add("park_uid", pfd.Uid);
                            r.add("parkid", park.Parkid);
                            r.add("carid", parkUserDataCar.Carid);
                            r.add("neighbor", isNeighbor ? "1" : "0");
                            r.add("acc", this._acc);
                            if (isFree)
                            {
                                r.add("first_free_parking", _parkUserData.User.First_free_parking.ToString());
                                _parkUserData.User.First_free_parking = 0;
                            }
                            else
                            {
                                r.add("first_fee_parking", _parkUserData.User.First_fee_parking.ToString());
                                _parkUserData.User.First_fee_parking = 0;
                            }

                            string parkRet = dac.PostRequest(URLS.停车地址, r.ToString());
                            ParkResult pr = 页面分析者.停车结果(parkRet);
                            park.Carid = parkUserDataCar.Carid;
                            if (pr.Errno == 0)//成功
                            {
                                parkUserDataCar.Park_profit = 10;
                                pfd.Full = pud.Full;//把当前车位的好友状态弄成相应的full状态
                                if (parkUserDataCar.Park_uid != null)//如果原来停在了一个车位上的话就设置 如果是正在找车位就没有这个动作
                                {
                                    ParkFriendData opfd = this.findParkFriendDataByID(parkUserDataCar.Park_uid);//把车位好友的状态弄成不满的状态
                                    opfd.Full = "0";
                                }
                                RaiseMsg(string.Format("[停车模块] {0}{1}[{2}]停在{3}的{4}号{5}车位上", "", parkUserDataCar.Car_name, parkUserDataCar.Car_trademarkname, pud.User.Real_name, park.Parkid, park.isFreePark ? "免费" : "收费[" + pfd.Scenemoney + "元/分钟]"));
                                return true;//直接退出就
                            }
                            else
                            {
                                //错误
                                RaiseMsg(string.Format("[停车模块] {0}{1}[{2}]停在{3}的{4}号{5}车位上停车失败:(" + pr.Errno + ")" + pr.Error, "", parkUserDataCar.Car_name, parkUserDataCar.Car_trademarkname, pud.User.Real_name, park.Parkid, park.isFreePark ? "免费" : "收费[" + pfd.Scenemoney + "元/分钟]"));
                                if (pr.Errno != 6 || pr.Errno != 5 || pr.Errno != 3) break;//如果不是同一好友上的停车就跳出 说明不能停该车
                            }
                        }
                    }

                }
            }
            return result;
        }

        #endregion 停车方法系列
        #region 拉力赛系列
        public void MatchPlayPlay()
        {
            //进入拉力赛主页
            string MatchMainPageString = dac.SimpleRequest(URLS.拉力赛首页);
            MatchInfo[] minfos = 页面分析者.拉力赛事列表(MatchMainPageString);
            //循环进入
            foreach (MatchInfo m in minfos)
            {
                RaiseMsg(string.Format("[拉力赛模块] {0}VS{1} {2}", m.FirstUserString, m.SecondUserString, m.DayString));
                dac.SimpleRequest(URLS.拉力赛首页);//先来一下拉力赛首页
                //进入每一对赛车详情页面
                请求参数 r = new 请求参数();
                r.add("matchuid", m.MatchUid);
                r.add("matchid", m.MatchID);
                string MatchDetailPageString = dac.GetRequest(URLS.拉力赛详情, r.ToString());
                ///拉力赛的各种情况
                ///拉力赛有几种状态 
                ///1刚刚发起的   
                ///2没启动赛车的 
                ///3已经启动赛车的 
                ///4赛车已经行驶完成的
                ///好友关系有几种
                ///1我参加的
                ///2一个好友的
                ///3两个都是好友的
                ///   2和3可以考虑成一种 同时给予加油就行了 不过要看好友列表中是否加过油了吗?
                ///   
                /// 程序进来先看拉力赛状态 如果能加油就加油 如果能启动那就启动赛车
                ///
                if (m.hasMyTeam(this.Account))//如果有我的队伍参赛就走我的流程
                {
                    if (StringHelper.Contains("你的车队今天还没启动", MatchDetailPageString))//没启动当然要启动了
                    {
                        string g_verify = StringHelper.getMiddleString("var g_verify = \"", "\";", MatchDetailPageString);
                        string g_matchuid = StringHelper.getMiddleString("var g_matchuid = \"", "\";", MatchDetailPageString);
                        string g_matchid = StringHelper.getMiddleString("var g_matchid = \"", "\";", MatchDetailPageString);
                        string[] teamInfo = StringHelper.getMiddleString("javascript:teamBegin(", ");\"", MatchDetailPageString).Split(',');
                        string teamUid = teamInfo[0];
                        string teamid = teamInfo[1];
                        请求参数 Beginr = new 请求参数();
                        Beginr.add("verify", g_verify);
                        Beginr.add("teamuid", teamUid);
                        Beginr.add("teamid", teamid);
                        string TeamBeginString = dac.GetRequest(URLS.拉力赛启动赛车, Beginr.ToString());
                        TeamBeginString += "";
                        RaiseMsg("[拉力赛模块]  启动赛车");
                    }
                    else if (StringHelper.Contains("你的车队今天已经启动", MatchDetailPageString))//今天已经启动 不用做任何工作了
                    {
                        RaiseMsg("[拉力赛模块] 你的车队今天已经启动");
                    }//其他情况还没有碰到 暂时先加到这里
                }
                else
                {
                    if (StringHelper.Contains("明日正式发车", MatchDetailPageString))
                    {
                        RaiseMsg("[拉力赛模块] 明日正式发车,今天不能加油");
                    }
                    else//剩下的情况都加油
                    {//考虑到联合车队的时候 加油的uid可能不是我的好友 而联合者是我的好友 这样的情况也能加油 所以现在整体都加油
                        string g_verify = StringHelper.getMiddleString("var g_verify = \"", "\";", MatchDetailPageString);
                        string g_matchuid = StringHelper.getMiddleString("var g_matchuid = \"", "\";", MatchDetailPageString);
                        string g_matchid = StringHelper.getMiddleString("var g_matchid = \"", "\";", MatchDetailPageString);
                        int pos = 0;
                        ///给第一队加油
                        string[] teamInfo = StringHelper.getMiddleString("javascript:oil(", " , 1);\"", MatchDetailPageString, ref pos).Split(',');
                        string teamUid = teamInfo[0];
                        string teamid = teamInfo[1];
                        请求参数 oilr = new 请求参数();
                        oilr.add("verify", g_verify);
                        oilr.add("teamuid", teamUid);
                        oilr.add("teamid", teamid);
                        oilr.add("type", "1");
                        oilr.add("matchuid", g_matchuid);
                        oilr.add("matchid", g_matchid);
                        HttpWebResponse resp = dac.GetRequestResp(URLS.拉力赛加油, oilr.ToString());

                        ///这地方那个很怪异 失败的时候 请求了两次页面
                        int code = 页面分析者.拉力赛提示code(resp.ResponseUri.Query);
                        oilr = new 请求参数();
                        oilr.add("action", code.ToString());
                        oilr.add("teamuid", teamUid);
                        oilr.add("teamid", teamid);
                        oilr.add("verify", g_verify);
                        string requestString = resp.ResponseUri.Query;
                        resp.Close();
                        string oilretstring = dac.GetRequest(URLS.拉力赛加油, resp.ResponseUri.Query);
                        //RaiseMsg("[拉力赛模块] "+页面分析者.拉力赛提示信息(oilretstring,code));

                        //if (code == 0)
                        RaiseMsg("[拉力赛模块] 为" + m.FirstUserString + "加油成功");
                        //else RaiseMsg("[拉力赛模块] 当前不能给" + m.FirstUserString + "加油或者已经加过油了");
                        ///跳过捣鬼这里的代码
                        StringHelper.getMiddleString("javascript:oil(", " , 0);\" class=\"gg\">捣鬼", MatchDetailPageString, ref pos);
                        ///给第二队加油
                        teamInfo = StringHelper.getMiddleString("javascript:oil(", " , 1);\"", MatchDetailPageString, ref pos).Split(',');
                        teamUid = teamInfo[0];
                        teamid = teamInfo[1];
                        oilr = new 请求参数();
                        oilr.add("verify", g_verify);
                        oilr.add("teamuid", teamUid);
                        oilr.add("teamid", teamid);
                        oilr.add("type", "1");
                        oilr.add("matchuid", g_matchuid);
                        oilr.add("matchid", g_matchid);
                        resp = dac.GetRequestResp(URLS.拉力赛加油, oilr.ToString());

                        ///这地方那个很怪异 失败的时候 请求了两次页面
                        code = 页面分析者.拉力赛提示code(resp.ResponseUri.Query);
                        oilr = new 请求参数();
                        oilr.add("action", code.ToString());
                        oilr.add("teamuid", teamUid);
                        oilr.add("teamid", teamid);
                        oilr.add("verify", g_verify);
                        requestString = resp.ResponseUri.Query;
                        resp.Close();
                        oilretstring = dac.GetRequest(URLS.拉力赛加油, resp.ResponseUri.Query);
                        //RaiseMsg("[拉力赛模块] "+页面分析者.拉力赛提示信息(oilretstring,code));

                        //if (code == 0)
                        RaiseMsg("[拉力赛模块] 为" + m.SecondUserString + "加油成功");
                        //else RaiseMsg("[拉力赛模块] 当前不能给" + m.SecondUserString + "加油或者已经加过油了");


                    }

                }
            }


        }





        #endregion 拉力赛系列


    }
}
